/*
* Effects (Refactored)@VERSION
*
* Copyright XXXX, COPYRIGHT HOLDERS
*
* Depends:
* jquery.pajinate.min.js
*/

var API = API || {};

API.x_effects = {

	EffectsManager: function() {
		var parent = this;

		// effects settings
		var dropScrollTime = 1300;
		var delayTime = 70;

    	this.Init = function(mask_object, pager_object) {
    		parent.mask_object = mask_object;
    		parent.pager_object = pager_object;
    	};
    	
    	this.preEffects = function() {
    		console.log('preEffects start');
    		if ($(parent.mask_object).isMasked()) {
                return false;
            }
    		console.log('preEffects after ');
    		$(parent.mask_object).mask(lang['filter.filtering'] + "...");

    		var target = document.querySelector('.loadmask-msg');
    		var observer = null;
    		console.log('preEffects target:', target);
    		// create an observer instance
    		observer = new MutationObserver(function(mutations) {
    			observer.disconnect();
    			is_filtering = false;
    			console.log('observer end');
    		});
    		 
    		// configuration of the observer:
    		var config = { attributes: true, childList: true, characterData: true };
 
    		// pass in the target node, as well as the observer options
    		observer.observe(target, config);
    		 
    		

		};

    	this.preEffects2 = function() {
    		console.log('preEffects start');
    		if ($(parent.mask_object).isMasked()) {
                return false;
            }
    		console.log('preEffects after ');
    		$(parent.mask_object).mask(lang['filter.filtering'] + "...");
    		
    		// When the DOM of the loading mask is over we declare filter finished 
    		// Deprecated event DOMNodeRemoved
    		document.body.addEventListener('DOMNodeRemoved', function(evt){
    			if($(evt.target).attr('class') === 'loadmask-msg'){
    				console.log('DOMNodeRemoved:', $(evt.target).attr('class'));	
    				is_filtering = false;
    				document.removeEventListener('DOMNodeRemoved');
    			}
    		}, false);
    	}
		
		this.afterEffects = function(callback, args) {
    		parent.callback = callback;

    		var randomnumber = delayTime + Math.floor(Math.random() * 501);

			$.scrollTo(parent.pager_object, dropScrollTime, {onAfter: function() {
				 window.setTimeout(function() {
					 if(parent.callback){
						 parent.callback.apply(this, args || []);
					 }
					 
					 $(parent.mask_object).unmask();
                 }, randomnumber);
                 return false;
			}});
		};

		this.afterEffects2 = function() {
			var randomnumber = delayTime + Math.floor(Math.random() * 501);

			$.scrollTo(parent.pager_object, dropScrollTime, {onAfter: function() {
				 window.setTimeout(function() {
					 console.log('afterEffects2 end');
					 $(parent.mask_object).unmask();
//					 is_filtering = false;
                 }, randomnumber);
                 return false;
			}});
		};
		
    }
};
